/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName CurrencyHandler.as
 * @creationDate Aug 16, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.model.initialsetup.unversionedxmlhandlers.handlers
{
	import com.gesturesoft.imultimax.events.CustomErrorEvent;
	import com.gesturesoft.imultimax.model.DataBaseManager;
	import com.gesturesoft.imultimax.model.error.DataBaseError;
	import com.gesturesoft.imultimax.model.interfaces.IDatabaseManager;
	import com.gesturesoft.imultimax.model.loader.events.LoadResultEvent;
	import com.gesturesoft.imultimax.model.utility.XMLUtilityParser;
	import com.gesturesoft.imultimax.model.vo.CurrencyVO;
	
	import mx.collections.ArrayCollection;

	public class CurrencyHandler
	{
		
		
		/**
		 * This is the function for insert currency details to database
		 * 
		 * @functionName insertCurrency
		 * @creationDate Aug 16, 2012
		 * @author micmac
		 * @params event:LoadResultEvent
		 */
		public function insertCurrency(event:LoadResultEvent):void
		{
			
			var xmlParser:XMLUtilityParser = new XMLUtilityParser();
			var currencies:ArrayCollection = xmlParser.parseCurrencyCodes(event.data);
			
			//START:Insert currency codes to database
			var dbManager:IDatabaseManager = new DataBaseManager();
			dbManager.setTransactionStartPoint();
			for each(var currency:CurrencyVO in currencies)
			{
				
				//dbManager.prepareTable("currencies");
				var query:String = "INSERT INTO IM_CURRENCIES(cur_label, cur_country) VALUES('"+currency.label+"','"+currency.country+"' )";
				try
				{
					
					dbManager.execute(query);
					
				} 
				catch(error:DataBaseError) 
				{
					
					dbManager.rollBackTransactions();
					throw error;
					//setErrorDisplay("Currency codes insertion failed");
					return;
					
				}
				
			}
			dbManager.commitTransaction();
			//END:Insert currency codes to database
			
			
		}
		
		
		
	}
}